package com.forlove.app.manager;

import android.net.Uri;

import com.forlove.app.common.base.CommonConfig;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.LubanOptions;
import com.jph.takephoto.model.TakePhotoOptions;

import java.io.File;


/**
 * - 支持通过相机拍照获取图片
 * - 支持从相册选择图片
 * - 支持从文件选择图片
 * - 支持多图选择
 * - 支持批量图片裁切
 * - 支持批量图片压缩
 * - 支持对图片进行压缩
 * - 支持对图片进行裁剪
 * - 支持对裁剪及压缩参数自定义
 * - 提供自带裁剪工具(可选)
 * - 支持智能选取及裁剪异常处理
 * - 支持因拍照Activity被回收后的自动恢复
 * GitHub:https://github.com/crazycodeboy
 */
public class CustomHelper {

    private static CustomHelper customHelper;

    private CustomHelper() {
    }

    public static synchronized CustomHelper getCustomHelper() {

        if (customHelper == null) {
            customHelper = new CustomHelper();
        }
        return customHelper;
    }

    public Uri getURI(TakePhoto takePhoto) {
        File file = new File(CommonConfig.pmImageDir, "/temp/" + System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        Uri imageUri = Uri.fromFile(file);
        configTakePhotoOption(takePhoto);
        configCompress(takePhoto, true);
        return imageUri;
    }

    /**
     * 配置option
     *
     * @param takePhoto
     */
    private void configTakePhotoOption(TakePhoto takePhoto) {
        TakePhotoOptions.Builder builder = new TakePhotoOptions.Builder();
        builder.setWithOwnGallery(false); //是否使用TakePhoto相册
        builder.setCorrectImage(true);
        takePhoto.setTakePhotoOptions(builder.create());

    }

    /**
     * 当系统中没有自带或第三方裁剪工具时，TakePhoto会自动切换到使用TakePhoto自带的裁剪工具进行裁剪。
     * <p>
     * config.enableReserveRaw(boolean)
     * true保留原图，false删除原图
     *
     * @param takePhoto
     */
    private void configCompress(TakePhoto takePhoto, boolean compressWithOwn) {
        int maxSize = 102400;//
        int width = 800;
        int height = 800;
        boolean showProgressBar = true;
        boolean enableRawFile = true;
        CompressConfig config;

        if (compressWithOwn) {
            config = new CompressConfig.Builder()
                    .setMaxSize(maxSize)
                    .setMaxPixel(width >= height ? width : height)
                    .enableReserveRaw(enableRawFile)
                    .enablePixelCompress(true)
                    .create();
        } else {
            LubanOptions option = new LubanOptions.Builder()
                    .setMaxHeight(height)
                    .setMaxWidth(width)
                    .setMaxSize(maxSize)
                    .create();
            config = CompressConfig.ofLuban(option);
            //是否删除原图
            config.enableReserveRaw(enableRawFile);
        }

        takePhoto.onEnableCompress(config, showProgressBar);

    }

    /**
     * 裁剪工具配置
     *
     * @return
     */
    public CropOptions getCropOptions() {
        int height = 800;
        int width = 800;
        boolean withWonCrop = true;
        CropOptions.Builder builder = new CropOptions.Builder();
        builder.setAspectX(width).setAspectY(height);
        builder.setOutputX(width).setOutputY(height);
        builder.setWithOwnCrop(withWonCrop);
        return builder.create();
    }
}
